package com.tmeone.erpone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tmeone.erpone.entity.*;
import com.tmeone.erpone.mapper.*;
import com.tmeone.erpone.service.ITOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tmeone
 * @since 2021-04-19
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements ITOrderService {

    @Autowired
    private TOrderMapper orderMapper;

    @Autowired
    private SCompanyMapper companyMapper;

    @Autowired
    private SUserMapper userMapper;

    @Autowired
    private TProductMapper productMapper;

    @Autowired
    private TCategoriesMapper categoriesMapper;

    @Autowired
    private TOrderStatusMapper orderStatusMapper;

    @Autowired
    private TProductionLineMapper productionLineMapper;

    //分页,状态,模糊综合查询
    @Override
    public IPage<TOrder> getPageAll(IPage<TOrder> page, String orderState, String orderId, String dealTime) {
        return orderMapper.selectPageAll(page,orderState,orderId,dealTime);
    }

    //根据日期或产能排序
    @Override
    public IPage<TOrder> getSortPage(IPage<TOrder> page, String list) {
        return orderMapper.selectSortPage(page,list);
    }

    //订单修改
    @Override
    public void updOrder(TOrder order) {
        orderMapper.updOrder(order);
    }

    /**
     * 获取所有订单
     * 作者：wzs
     * @param page 当前页数
     * @param orderType 订单类型
     * @param orderStatus 订单状态
     * @param orderId 订单id
     * @param dealTime 交易时间
     * @return 所有订单
     */
    @Override
    public IPage<TOrder> selectAllByOrderTypeAndOrderStatusAndOrderIdLikeAndDealTime(Page<TOrder> page, String orderType, String orderStatus, String orderId, String dealTime) {
        IPage<TOrder> orderPage = orderMapper.selectAllByOrderTypeAndOrderStatusAndOrderIdLikeAndDealTime(page, orderType, orderStatus, orderId, dealTime);
        for (TOrder order : orderPage.getRecords()) {

            //添加下单公司
            SCompany sCompany = companyMapper.selectOne(new QueryWrapper<SCompany>().lambda().eq(SCompany::getId, order.getCustomId()));
            if (sCompany != null) {
                order.setCompany(sCompany);
            }

            //添加供应商公司
            SCompany company = companyMapper.selectOne(new QueryWrapper<SCompany>().lambda().eq(SCompany::getId, order.getSupplierId()));
            if (company != null) {
                order.setSupplierCompany(company);
            }

            //添加用户
            SUser user = userMapper.selectOne(new QueryWrapper<SUser>().lambda().eq(SUser::getId, order.getOrderHolder()));
            if (user != null){
                order.setUser(user);
            }

            //添加商品
            TProduct tProduct = productMapper.selectOne(new QueryWrapper<TProduct>().lambda().eq(TProduct::getId, order.getProductId()));
            if (tProduct != null) {
                //商品类型
                TCategories tCategories = categoriesMapper.selectOne(new QueryWrapper<TCategories>().lambda().eq(TCategories::getId, tProduct.getPEnterprise()));
                if (tCategories != null) {
                    tProduct.setPEnterprise(tCategories.getName());
                }
                order.setProduct(tProduct);
            }

            //添加订单状态
            TOrderStatus os = orderStatusMapper.selectOne(new QueryWrapper<TOrderStatus>().lambda().eq(TOrderStatus::getId, order.getOrderStatus()));
            if (os != null) {
                order.setT_orderStatus(os);
            }

            //添加产能
            TProductionLine productionLine = productionLineMapper.selectOne(new QueryWrapper<TProductionLine>().lambda().eq(TProductionLine::getId, order.getProductLineId()));
            if (productionLine != null) {
                order.setProductionLine(productionLine);
            }
        }
        return orderPage;
    }
    @Autowired
    private TOrderMapper tOrderMapper;

    @Override
    //代发货订单详情分页查询
    public IPage<TOrder> pageTOrderGo(IPage<TOrder> page) {
        return tOrderMapper.pageTOrderGo(page);
    }

    @Override
    public List<TOrder> selectOrderByOrderId(String orderId) {
        return tOrderMapper.selectOrderByOrderId(orderId);
    }

}
